<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="手机号码" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="queryParams.password"
          placeholder="请输入密码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select v-model="queryParams.sex" placeholder="请选择性别" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="出生年月日" prop="birthday">
        <el-date-picker clearable size="small"
          v-model="queryParams.birthday"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择出生年月日">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input
          v-model="queryParams.avatar"
          placeholder="请输入头像"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="E-Mail" prop="email">
        <el-input
          v-model="queryParams.email"
          placeholder="请输入E-Mail"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="注册日期" prop="regdate">
        <el-date-picker clearable size="small"
          v-model="queryParams.regdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择注册日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改日期" prop="updatedate">
        <el-date-picker clearable size="small"
          v-model="queryParams.updatedate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择修改日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后登陆日期" prop="lastdate">
        <el-date-picker clearable size="small"
          v-model="queryParams.lastdate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择最后登陆日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="在线时长" prop="online">
        <el-input
          v-model="queryParams.online"
          placeholder="请输入在线时长"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="兴趣" prop="interest">
        <el-input
          v-model="queryParams.interest"
          placeholder="请输入兴趣"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="个性" prop="personality">
        <el-input
          v-model="queryParams.personality"
          placeholder="请输入个性"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="粉丝数" prop="fanscount">
        <el-input
          v-model="queryParams.fanscount"
          placeholder="请输入粉丝数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关注数" prop="followcount">
        <el-input
          v-model="queryParams.followcount"
          placeholder="请输入关注数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="访问令牌" prop="accessToken">
        <el-input
          v-model="queryParams.accessToken"
          placeholder="请输入访问令牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="过期时间" prop="expiresIn">
        <el-input
          v-model="queryParams.expiresIn"
          placeholder="请输入过期时间"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="uid" prop="uid">
        <el-input
          v-model="queryParams.uid"
          placeholder="请输入uid"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['user:user:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['user:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['user:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['user:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="手机号码" align="center" prop="mobile" />
      <el-table-column label="密码" align="center" prop="password" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="性别" align="center" prop="sex" />
      <el-table-column label="出生年月日" align="center" prop="birthday" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" prop="avatar" />
      <el-table-column label="E-Mail" align="center" prop="email" />
      <el-table-column label="注册日期" align="center" prop="regdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.regdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改日期" align="center" prop="updatedate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登陆日期" align="center" prop="lastdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="在线时长" align="center" prop="online" />
      <el-table-column label="兴趣" align="center" prop="interest" />
      <el-table-column label="个性" align="center" prop="personality" />
      <el-table-column label="粉丝数" align="center" prop="fanscount" />
      <el-table-column label="关注数" align="center" prop="followcount" />
      <el-table-column label="访问令牌" align="center" prop="accessToken" />
      <el-table-column label="过期时间" align="center" prop="expiresIn" />
      <el-table-column label="uid" align="center" prop="uid" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['user:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['user:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号码" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="出生年月日" prop="birthday">
          <el-date-picker clearable size="small"
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择出生年月日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="E-Mail" prop="email">
          <el-input v-model="form.email" placeholder="请输入E-Mail" />
        </el-form-item>
        <el-form-item label="注册日期" prop="regdate">
          <el-date-picker clearable size="small"
            v-model="form.regdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择注册日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="修改日期" prop="updatedate">
          <el-date-picker clearable size="small"
            v-model="form.updatedate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择修改日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后登陆日期" prop="lastdate">
          <el-date-picker clearable size="small"
            v-model="form.lastdate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择最后登陆日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="在线时长" prop="online">
          <el-input v-model="form.online" placeholder="请输入在线时长" />
        </el-form-item>
        <el-form-item label="兴趣" prop="interest">
          <el-input v-model="form.interest" placeholder="请输入兴趣" />
        </el-form-item>
        <el-form-item label="个性" prop="personality">
          <el-input v-model="form.personality" placeholder="请输入个性" />
        </el-form-item>
        <el-form-item label="粉丝数" prop="fanscount">
          <el-input v-model="form.fanscount" placeholder="请输入粉丝数" />
        </el-form-item>
        <el-form-item label="关注数" prop="followcount">
          <el-input v-model="form.followcount" placeholder="请输入关注数" />
        </el-form-item>
        <el-form-item label="访问令牌" prop="accessToken">
          <el-input v-model="form.accessToken" placeholder="请输入访问令牌" />
        </el-form-item>
        <el-form-item label="过期时间" prop="expiresIn">
          <el-input v-model="form.expiresIn" placeholder="请输入过期时间" />
        </el-form-item>
        <el-form-item label="uid" prop="uid">
          <el-input v-model="form.uid" placeholder="请输入uid" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mobile: null,
        password: null,
        nickname: null,
        sex: null,
        birthday: null,
        avatar: null,
        email: null,
        regdate: null,
        updatedate: null,
        lastdate: null,
        online: null,
        interest: null,
        personality: null,
        fanscount: null,
        followcount: null,
        accessToken: null,
        expiresIn: null,
        uid: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        mobile: null,
        password: null,
        nickname: null,
        sex: null,
        birthday: null,
        avatar: null,
        email: null,
        regdate: null,
        updatedate: null,
        lastdate: null,
        online: null,
        interest: null,
        personality: null,
        fanscount: null,
        followcount: null,
        accessToken: null,
        expiresIn: null,
        uid: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除用户编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delUser(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('user/user/export', {
        ...this.queryParams
      }, `user_user.xlsx`)
    }
  }
};
</script>